var html = document.documentElement
const layout = html.clientWidth || document.body.clientWidth;
html.style.fontSize = layout / 3.75 + 'px'
$('.chartUl').delegate(".minus", 'click', (e) => {
  let data = JSON.parse(localStorage.getItem($(e.currentTarget.parentElement).attr('gid')))
  let n = $(e.currentTarget.nextElementSibling).attr('value')
  if (n <= 0) {
    n = 0
  } else {
    n--
  }
  $(e.currentTarget.nextElementSibling).attr('value', n)
  localStorage.setItem($(e.currentTarget.parentElement).attr('gid'), JSON.stringify({
    num: n,
    url: data.url,
    price: data.price,
    name: data.name
  }))
  total()
})
$('.chartUl').delegate(".plus", 'click', (e) => {
  let data = JSON.parse(localStorage.getItem($(e.currentTarget.parentElement).attr('gid')))
  let n = $(e.currentTarget.previousElementSibling).attr('value')
  n++
  $(e.currentTarget.previousElementSibling).attr('value', n)
  localStorage.setItem($(e.currentTarget.parentElement).attr('gid'), JSON.stringify({
    num: n,
    url: data.url,
    price: data.price,
    name: data.name
  }))
  total()
})
$('.chartUl').delegate(".delete", 'click', (e) => {
  localStorage.removeItem($(e.currentTarget.parentElement).attr('gid'))
  $(e.currentTarget.parentElement).remove()
  if ($('.chartUl').children().length == 0) {
    $('.totalPrice').text(0)
  } else {
    total()
  }

})
for (let i = 0; i < localStorage.length; i++) {
  if (localStorage.key(i) === 'loglevel:webpack-dev-server') {
    continue
  }
  /*   if (i == localStorage.length - 1) {
      total()
    } */
  total()
  let detailslist = JSON.parse(localStorage.getItem(localStorage.key(i)))
  $('.chartUl').append(`
    <li class=goodDetail gid=${localStorage.key(i)}>
    <img src=${detailslist.url} class=goodImg>
    <p class=goodName>${detailslist.name}</p>
    <p class=goodPrice>￥${detailslist.price}.00</p>
    <div class=minus>-</div>
    <input type=text class=number value=${detailslist.num} disabled="disabled">
    <div class=plus>+</div>
    <div class=delete>删除商品</div>
    </li>
    `)
}

function total() {
  let t = 0
  let tt = 0
  for (let k = 0; k < localStorage.length; k++) {
    if (localStorage.key(k) === 'loglevel:webpack-dev-server') {
      continue
    }
    var data = localStorage.getItem(localStorage.key(k))
    if (typeof data === 'string') {
      var totaldata = JSON.parse(data)
    } else {
      totaldata = data
    }
    let x = totaldata.price
    let y = Number(totaldata.num)
    t = x * y
    tt += t
    $('.totalPrice').text('￥' + tt)
  }
}